{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "remove-cell"
    ]
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "\n",
    "import openpifpaf\n",
    "openpifpaf.show.Canvas.show = True"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# CrowdPose\n",
    "\n",
    "More info on the CrowdPose dataset: [arxiv.org/abs/1812.00324](https://arxiv.org/abs/1812.00324), [github.com/Jeff-sjtu/CrowdPose](https://github.com/Jeff-sjtu/CrowdPose).\n",
    "\n",
    "This page gives a quick introduction to OpenPifPaf's CrowdPose plugin that is part of `openpifpaf.plugins`.\n",
    "The plugin adds a `DataModule`. CrowdPose annotations are COCO-compatible, so this datamodule only has to configure the existing COCO dataset class.\n",
    "This plugin is quite small and might serve as a template for your custom plugin for other COCO-compatible datasets.\n",
    "Let's start with the setup for this notebook and register all available OpenPifPaf plugins:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(openpifpaf.plugin.REGISTERED.keys())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Inspect\n",
    "\n",
    "Next, we configure and instantiate the datamodule and look at the configured head metas:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "datamodule = openpifpaf.plugins.crowdpose.CrowdPose()\n",
    "print(datamodule.head_metas)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We see here that CrowdPose has CIF and CAF heads.\n",
    "\n",
    "Next, we want to visualize the pose:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# first make an annotation\n",
    "ann = openpifpaf.Annotation.from_cif_meta(datamodule.head_metas[0])\n",
    "\n",
    "# visualize the annotation\n",
    "openpifpaf.show.KeypointPainter.show_joint_scales = True\n",
    "keypoint_painter = openpifpaf.show.KeypointPainter()\n",
    "with openpifpaf.show.Canvas.annotation(ann) as ax:\n",
    "    keypoint_painter.annotation(ax, ann)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Prediction\n",
    "\n",
    "We use the pretrained model `resnet50-crowdpose`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%bash\n",
    "python -m openpifpaf.predict coco/000000081988.jpg --checkpoint=resnet50-crowdpose --image-output coco/000000081988.jpg.predictions-crowdpose.jpeg --image-min-dpi=200"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import IPython\n",
    "IPython.display.Image('coco/000000081988.jpg.predictions-crowdpose.jpeg')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Image credit: \"[Learning to surf](https://www.flickr.com/photos/fotologic/6038911779/in/photostream/)\" by fotologic which is licensed under [CC-BY-2.0]."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Dataset\n",
    "\n",
    "For training and evaluation, you need to download the dataset.\n",
    "\n",
    "```sh\n",
    "mkdir data-crowdpose\n",
    "cd data-crowdpose\n",
    "# download links here: https://github.com/Jeff-sjtu/CrowdPose\n",
    "unzip annotations.zip\n",
    "unzip images.zip\n",
    "```\n",
    "\n",
    "Now you can use the standard {ref}`openpifpaf.train <cli-help-train>` and \n",
    "{ref}`openpifpaf.eval <cli-help-eval>` commands as documented in {doc}`train`\n",
    "with `--dataset=crowdpose`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.9.2 64-bit ('venv3')",
   "name": "python37464bitvenv3venve864f70b47a24f709eace0523e013bb0"
  },
  "language_info": {
   "name": "python",
   "version": ""
  },
  "metadata": {
   "interpreter": {
    "hash": "ea6946363a43e80d241452ab397f4c58bdd3d2517da174158e9c46ce6717422a"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
